﻿using System.Xml;
using System.Xml.Linq;
using Xunit;
using Xunit.Should;

namespace Adjunct.Framework.Nuspec
{
	public class NuspecIncludeSpec
	{
		private const string ValidAllXml = @"<file src='takeThese*' target='relative' exclude='ignore\**\these' />";
		private const string ValidMinXml = @"<file src='takeThese*' />";
		private readonly NuspecInclude _validAll = new NuspecInclude("takeThese*", "relative", @"ignore\**\these");
		private readonly NuspecInclude _validMin = new NuspecInclude("takeThese*");

		[Fact]
		public void FromNullXElement()
		{
			XElement element = null;
			((NuspecInclude)element).ShouldBeNull();
		}

		[Fact]
		public void FromValidAllXElement()
		{
			((NuspecInclude)XElement.Parse(ValidAllXml)).ShouldEqual(_validAll);
		}

		[Fact]
		public void FromValidMinXElement()
		{
			((NuspecInclude)XElement.Parse(ValidMinXml)).ShouldEqual(_validMin);
		}

		[Fact]
		public void NullToXElement()
		{
			NuspecInclude include = null;
			((XElement)include).ShouldBeNull();
		}

		[Fact]
		public void MissingSourceToXElementThrows()
		{
			NuspecInclude include = new NuspecInclude();
			XmlException exception = Assert.Throws<XmlException>(() => (XElement)include);
			exception.Message.ShouldEqual("Can not create attribute 'src', the value was null.");
		}

		[Fact]
		public void ValidAllToXElement()
		{
			((XElement)_validAll).ShouldEqual(ValidAllXml);
		}

		[Fact]
		public void ValidMinToXElement()
		{
			((XElement)_validMin).ShouldEqual(ValidMinXml);
		}
	}
}